/**
 * 
 */
package edu.umd.clip.lm.tools;

import java.io.*;

import edu.berkeley.nlp.util.Option;
import edu.berkeley.nlp.util.OptionParser;
import edu.umd.clip.lm.model.*;
import edu.umd.clip.lm.util.IO;
import edu.umd.clip.lm.util.tree.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class ConvertTreeFormat {

	public static class Options {
        @Option(name = "-f", required = true, usage = "source file")
		public String from;      
        @Option(name = "-t", required = true, usage = "target file")
		public String to;      
        @Option(name = "-m", required = true, usage = "mode: xml2ser or ser2xml")
		public String mode;      
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String args[]) throws IOException, ClassNotFoundException {
        OptionParser optParser = new OptionParser(Options.class);
        Options opts = (Options) optParser.parse(args, true);
        
        BinaryTree<HistoryTreePayload> tree;
        if (opts.mode.equals("xml2ser")) {
        	tree = LanguageModel.loadXMLHistoryTree(opts.from);
        	System.out.println("loaded XML tree");
        	ObjectOutputStream oos = new ObjectOutputStream(IO.getOutputStream(opts.to));
        	oos.writeObject(tree);
        	oos.close();
        } else if (opts.mode.equals("ser2xml")) {
        	ObjectInputStream ois = new ObjectInputStream(IO.getInputStream(opts.from));
        	tree = (BinaryTree<HistoryTreePayload>) ois.readObject();
        	ois.close();
        	System.out.println("loaded serialized tree");
        	LanguageModel.saveXMLHistoryTree(tree, opts.to);
        }
	}

}
